package org.zizi.service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.zizi.action.Action;
import org.zizi.action.Result;
import org.zizi.action.UserException;
import org.zizi.manager.Managers;
import org.zizi.manager.impl.ActionManager;


/**
 * 
 * 处理从form提交来的请求
 * 
 * @author zizi
 *
 */
public class DwrActionService 
{
	private static Logger log = Logger.getLogger(DwrActionService.class);
	
	public <T extends Action> Result<T> call(T action)
	{
		Result<T> result = new Result<T>();
		
		ActionManager am = Managers.get(ActionManager.class);
		try
		{
			WebContext wc = WebContextFactory.get();
			HttpServletRequest request = wc.getHttpServletRequest();
			HttpServletResponse response = wc.getHttpServletResponse();
			
			am.execute(action, request, response);
			
			result.setData(action);
		}
		catch (UserException e)
		{
			result.setError(e.getMessage());
		}
		catch (Exception e)
		{
			result.setError(e.getMessage());
			log.error("系统错误：", e);
		}
		
		return result;
	}
}
